import 'package:flutter/material.dart';
import 'package:flutter_cli/api/api.dart';
import 'package:flutter_cli/theme/theme.dart';
import 'package:flutter_cli/utils/common.dart';
import 'package:flutter_cli/utils/hk_scroll_view.dart';
import 'package:flutter_cli/widgets/widget.dart';

class MainTabPage extends StatelessWidget {
  // Tab页面
  static const String routeName = "/main_tab";

  const MainTabPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          appBar: mainBar(
            '首页',
            textColor: global.theme.value == ThemeStyle.light.value
                ? HexString("#555555")
                : Colors.white,
            backGround: global.theme.value == ThemeStyle.light.value
                ? HexString("#F5F5F5")
                : HexString("#090909"),
            actionsList: [
              InkWell(
                onTap: () async {
                  ThemeUtils.changeTheme();
                },
                child: Container(
                  margin: EdgeInsets.only(right: 30.w),
                  child: Icon(
                    Icons.color_lens_outlined,
                    size: 50.sp,
                  ),
                ),
              )
            ],
          ),
          body: HkScrollView(
            children: [
              Container(
                child: Center(
                  child: Text('MainTab'),
                ),
              ),
              _buildLoadingWidget(),
              _buildCommonDialog(),
              _buildRequestApi()
            ],
          ),
        ));
  }

  _buildLoadingWidget() {
    return GestureDetector(
      child: Container(
        height: 80.w,
        margin: EdgeInsets.only(top: 30.w),
        child: Center(
          child: Text(
            'SHOW_LOADING',
            style: TextStyle(
                fontSize: 30.sp, color: Get.textTheme.titleMedium?.color),
          ),
        ),
      ),
      onTap: () {
        LoadingUtils.show();
        hkSetTimeout(() {
          LoadingUtils.dismiss();
        }, const Duration(milliseconds: 500));
      },
    );
  }

  _buildCommonDialog() {
    return GestureDetector(
      child: Container(
        height: 80.w,
        margin: EdgeInsets.only(top: 30.w),
        child: Center(
          child: Text(
            '通用Dialog',
            style: TextStyle(
                fontSize: 30.sp, color: Get.textTheme.displaySmall?.color),
          ),
        ),
      ),
      onTap: () {
        showAlert(
          Get.context,
          confirm: () {},
          content: "别忘记下班打卡!!!",
        );
      },
    );
  }

  _buildRequestApi() {
    return GestureDetector(
      child: Container(
        height: 80.w,
        margin: EdgeInsets.only(top: 30.w),
        child: Center(
          child: Text(
            'RequestApi',
            style: TextStyle(
                fontSize: 30.sp, color: Get.textTheme.displaySmall?.color),
          ),
        ),
      ),
      onTap: () {
        Utils.throttle(func: () async {
          DioUtil().getHttp('article/listproject/0/json', {}, (val) {
            print("请求成功~");
            hkShowToast("请求成功~");
          }, showLoading: true);
        });
      },
    );
  }
}
